import { getVolunteerList } from '../../utils/volunteer';
Page({
  data: {
    volunteersList: []
  },

  onLoad: function () {
    // 页面加载时获取数据
    this.loadVolunteerData();
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    // 重新加载数据
    this.loadVolunteerData(() => {
      wx.stopPullDownRefresh();
    });
  },

  // 加载志愿者数据
  loadVolunteerData: function (callback) {
    // 调用接口
    getVolunteerList().then(res => {
      // console.log(res)
      this.setData({
        volunteersList: res
      })
      if (callback) callback();
    })
  },

  // 点击志愿者项
  onSelectVolunteer: function (e) {
    const volunteerId = e.currentTarget.dataset.id;
    const volunteer = this.data.volunteersList.find(item => item.uid === volunteerId);

    if (volunteer) {
      // console.log('已选择志愿者:', volunteer);

      // 跳转到志愿者详情页
      wx.navigateTo({
        url: './detail/detail?id=' + volunteerId,
        fail: function () {
          wx.showToast({
            title: '该功能正在开发中',
            icon: 'none',
            duration: 2000
          });
        }
      });
    }
  },

  // 添加新志愿者
  addNewVolunteer: function () {
    console.log('添加新志愿者');
    wx.navigateTo({
      url: './add/add'
    });
  },
    // 跳转到任务编辑页面
  onAddTask: function (e) {
    // const id = e.currentTarget.dataset.id;
    const id = 'test1';
    wx.navigateTo({
      url: `/pages/volunteers/addTask/addTask?id=${id}`,
    });
  },
}) 